<template>
  <div class="login-container">
    <div class="background-image"></div>
    <div class="login-box">
      <!-- 左侧背景 -->
      <div class="left-panel">
        <div class="welcome-text">
          <h2>欢迎使用</h2>
          <p>高效评测大模型的平台</p>
          <div class="decoration-circles">
            <div class="circle circle-1"></div>
            <div class="circle circle-2"></div>
            <div class="circle circle-3"></div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="right-panel">
        <div class="form-wrapper">
          <h2 class="title">{{ isRegister ? "注册账号" : "账号登录" }}</h2>
          <p class="subtitle">{{ isRegister ? "创建您的账号以开始使用" : "欢迎回来，请登录您的账号" }}</p>

          <el-form ref="formRef" :model="form" :rules="rules" label-position="top" class="login-form">
            <el-form-item label="用户名" prop="username">
              <el-input
                v-model="form.username"
                prefix-icon="el-icon-user"
                placeholder="请输入用户名"
                class="input-box"
              />
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input
                v-model="form.password"
                type="password"
                show-password
                prefix-icon="el-icon-lock"
                placeholder="请输入密码"
                class="input-box"
              />
            </el-form-item>

            <el-form-item v-if="isRegister" label="确认密码" prop="rePassword">
              <el-input
                v-model="form.rePassword"
                type="password"
                show-password
                prefix-icon="el-icon-lock"
                placeholder="请再次输入密码"
                class="input-box"
              />
            </el-form-item>

            <el-form-item v-if="!isRegister" class="login-options">
              <el-checkbox v-model="rememberMe" class="custom-checkbox">记住我</el-checkbox>
              <el-link type="primary" class="forgot-password">忘记密码？</el-link>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleSubmit" class="submit-btn" :loading="loading">
                {{ isRegister ? "创建账号" : "登录" }}
              </el-button>
            </el-form-item>
          </el-form>

          <div class="form-footer">
            <span>{{ isRegister ? "已有账号？" : "没有账号？" }}</span>
            <el-link type="primary" @click="toggleForm" class="toggle-link">
              {{ isRegister ? "去登录" : "去注册" }}
            </el-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import { loginUser, registerUser } from "@/api/User";
import store from "@/store"; // 引入 Vuex
export default {
  data() {
    return {
      isRegister: false, // 切换登录/注册
      rememberMe: false, // 记住我
      form: {
        username: "",
        password: "",
        rePassword: "",
      },
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        rePassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value !== this.form.password) {
                callback(new Error("两次输入的密码不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          if (this.isRegister) {
            this.register();
          } else {
            this.login();
          }
        }
      });
    },

    async login() {
      try {
        const response = await loginUser({
          username: this.form.username,
          password: this.form.password,
        });

        if (response.code === 0) {
          this.$message.success("登录成功！");
          const token = response.data;
          store.commit("setToken", token);
          this.$router.push("/dashboard");
        } else {
          this.$message.error(response.message || "登录失败");
        }
      } catch (error) {
        console.error("登录请求错误:", error);
        this.$message.error("网络错误，请稍后再试");
      }
    },

    async register() {
      try {
        const response = await registerUser({
          username: this.form.username,
          password: this.form.password,
        });

        console.log("注册响应:", response);
        if (response.code === 0) {
          this.$message.success("注册成功！");
          this.isRegister = false;
          this.form = { username: "", password: "", rePassword: "" };
          this.$refs.formRef.clearValidate();
        } else {
          this.$message.error(response.message || "注册失败");
        }
      } catch (error) {
        console.error("注册请求错误:", error);
        this.$message.error("网络错误，请稍后再试");
      }
    },

    toggleForm() {
      this.isRegister = !this.isRegister;
      this.form = { username: "", password: "", rePassword: "" };
      this.$refs.formRef.clearValidate();
    },
  },
};
</script>

<style scoped>
/* 全局背景 */
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  width: 100vw;
  position: relative;
  font-family: "PingFang SC", "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;
  overflow: hidden;
}

/* 背景图片 */
.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('../assets/img/background.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  filter: brightness(0.8); /* 稍微调暗背景，使登录框更突出 */
  z-index: 0;
}

/* 可选：添加一个覆盖层，使背景更暗，登录框更突出 */
.background-image::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4); /* 黑色半透明覆盖 */
  z-index: 1;
}

/* 登录框 */
.login-box {
  width: 900px;
  height: 650px;
  display: flex;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  transition: all 0.3s ease;
  position: relative;
  z-index: 2; /* 确保登录框在背景图片上方 */
}

/* 左侧背景 */
.left-panel {
  flex: 1;
  background: linear-gradient(135deg, #3a7bd5, #00d2ff);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.welcome-text {
  position: relative;
  z-index: 2;
}

.welcome-text h2 {
  font-size: 36px;
  font-weight: 600;
  margin-bottom: 16px;
  letter-spacing: 1px;
}

.welcome-text p {
  font-size: 20px;
  margin-top: 10px;
  opacity: 0.9;
  font-weight: 300;
  letter-spacing: 0.5px;
}

/* 装饰圆圈 */
.decoration-circles {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: -50px;
  left: -50px;
}

.circle-2 {
  width: 300px;
  height: 300px;
  bottom: -100px;
  right: -100px;
}

.circle-3 {
  width: 150px;
  height: 150px;
  bottom: 50px;
  left: 30px;
}

/* 右侧表单 */
.right-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95); /* 稍微透明，可以看到一点背景 */
}

.form-wrapper {
  width: 100%;
  max-width: 360px;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  text-align: left;
}

.subtitle {
  font-size: 16px;
  color: #666;
  margin-bottom: 30px;
  text-align: left;
}

.login-form {
  text-align: left;
}

/* 输入框 */
.input-box {
  border-radius: 8px;
  height: 46px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.input-box:focus {
  border-color: #3a7bd5;
  box-shadow: 0 0 0 2px rgba(58, 123, 213, 0.2);
}

.el-form-item__label {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  padding-bottom: 8px;
}

/* 复选框 & 忘记密码 */
.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.custom-checkbox {
  font-size: 14px;
  color: #666;
}

.forgot-password {
  font-size: 14px;
  color: #3a7bd5;
}

/* 提交按钮 */
.submit-btn {
  width: 100%;
  height: 46px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #3a7bd5, #00d2ff);
  border: none;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(58, 123, 213, 0.3);
}

.submit-btn:active {
  transform: translateY(0);
}

/* 注册/登录切换 */
.form-footer {
  margin-top: 24px;
  text-align: center;
  font-size: 14px;
  color: #666;
}

.toggle-link {
  font-weight: 500;
  margin-left: 5px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    height: auto;
    flex-direction: column;
  }
  
  .left-panel {
    padding: 40px 20px;
  }
  
  .right-panel {
    padding: 30px 20px;
  }
  
  .form-wrapper {
    max-width: 100%;
  }
}
</style>